package com.xayy.core.antlr4.utils;

import cn.kawins.base.utils.CollUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/8/17 10:24
 */
public class Golabl {
    /**
     * 正常计算的指标缓存
     */
    public static final Map<Long, Map<Long, Map<String, Map<String, Double>>>> PARAM_VALUE = new ConcurrentHashMap<>();
    /**
     * 异常计算时候的指标缓存
     */
    public static final Map<Long, Map<Long, Map<String, Map<String, Double>>>> REEOR_PARAM_VALUE =
        new ConcurrentHashMap<>();

    /**
     * 设置指标的分值缓存
     * 
     * @param satelliteId
     *            卫星id
     * @param name
     *            指标代号名称
     * @param type
     *            指标类型 ValueTypeEnum
     * @param value
     *            指标值
     */
    public static void setValue(Long satelliteId, Long nodeId, String name, Double value, String type) {
        Map<Long, Map<String, Map<String, Double>>> satMap = PARAM_VALUE.get(satelliteId);
        if (CollUtil.isEmpty(satMap))
            satMap = new ConcurrentHashMap<>();
        Map<String, Map<String, Double>> nodeMap = satMap.get(nodeId);
        if (CollUtil.isEmpty(nodeMap))
            nodeMap = new ConcurrentHashMap<>();
        Map<String, Double> jkMap = nodeMap.get(type);
        if (CollUtil.isEmpty(jkMap))
            jkMap = new ConcurrentHashMap<>();
        jkMap.put(name, value);
        nodeMap.put(type,jkMap);
        satMap.put(nodeId,nodeMap);
        PARAM_VALUE.put(satelliteId,satMap);
    }

    /**
     * 设置历史故障计算下的指标的分值缓存
     *
     * @param satelliteId
     *            卫星id
     * @param name
     *            指标代号名称
     * @param type
     *            指标类型 ValueTypeEnum
     * @param value
     *            指标值
     */
    public static void setErrorValue(Long satelliteId, Long nodeId, String name, Double value, String type) {
        Map<Long, Map<String, Map<String, Double>>> satMap = REEOR_PARAM_VALUE.get(satelliteId);
        if (CollUtil.isEmpty(satMap))
            satMap = new ConcurrentHashMap<>();
        Map<String, Map<String, Double>> nodeMap = satMap.get(nodeId);
        if (CollUtil.isEmpty(nodeMap))
            nodeMap = new ConcurrentHashMap<>();
        Map<String, Double> jkMap = nodeMap.get(type);
        if (CollUtil.isEmpty(jkMap))
            jkMap = new ConcurrentHashMap<>();
        jkMap.put(name, value);
        nodeMap.put(type,jkMap);
        satMap.put(nodeId,nodeMap);
        REEOR_PARAM_VALUE.put(satelliteId,satMap);
    }

    /**
     * 获取指标分值缓存
     * 
     * @param satelliteId
     *            卫星id
     * @param name
     *            指标代号名称
     * @param type
     *            指标类型 ValueTypeEnum
     * @return 结果
     */
    public static Double getVaule(Long satelliteId, Long nodeId, String name, String type) {
        Double value = null;
        Map<Long, Map<String, Map<String, Double>>> satMap = PARAM_VALUE.get(satelliteId);
        if (CollUtil.isEmpty(satMap))
            return value;
        Map<String, Map<String, Double>> nodeMap = satMap.get(nodeId);
        if (CollUtil.isEmpty(nodeMap))
            return value;
        Map<String, Double> jkMap = nodeMap.get(type);
        if (CollUtil.isEmpty(jkMap))
            return value;
        value = jkMap.get(name);
        return value;
    }

    /**
     * 获取历史故障计算下的指标分值缓存
     *
     * @param satelliteId
     *            卫星id
     * @param name
     *            指标代号名称
     * @param type
     *            指标类型 ValueTypeEnum
     * @return 结果
     */
    public static Double getErrorVaule(Long satelliteId, Long nodeId, String name, String type) {
        Double value = null;
        Map<Long, Map<String, Map<String, Double>>> satMap = REEOR_PARAM_VALUE.get(satelliteId);
        if (CollUtil.isEmpty(satMap))
            return value;
        Map<String, Map<String, Double>> nodeMap = satMap.get(nodeId);
        if (CollUtil.isEmpty(nodeMap))
            return value;
        Map<String, Double> jkMap = nodeMap.get(type);
        if (CollUtil.isEmpty(jkMap))
            return value;
        value = jkMap.get(name);
        return value;
    }

    public static void clear(Long satelliteId) {
        PARAM_VALUE.put(satelliteId, new ConcurrentHashMap<>());
    }

    public static void clearError(Long satelliteId) {
        REEOR_PARAM_VALUE.put(satelliteId, new ConcurrentHashMap<>());
    }

}
